//
//  BoutiqueCourseView.m
//  BeanSproutEDU
//
//  Created by douya on 15/2/10.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "BoutiqueCourseView.h"
#import "BCTableViewCell.h"
#import "DouyaPlayerViewController.h"

#import "MainModel.h"
#import "VideoModel.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "SDImageCache.h"
#import "Header.h"
@interface BoutiqueCourseView()

@end

@implementation BoutiqueCourseView



-(id)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        
        tableView.delegate=self;
        tableView.dataSource=self;
        tableView.rowHeight=100;
        _tableView=tableView;
        [self addSubview:tableView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"reloadData" object:nil];
    }
    return self;
}
-(void)reloadData:(NSNotification *)noti
{
    NSLog(@"更新数据了");
    NSDictionary *dict=(NSDictionary *)noti.object;
    NSMutableArray *array=[dict objectForKey:@"New"];
    self.dataArray=array;
    [_tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(BCTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BCTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"BCCell"];
    if (!cell)
    {
        NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"BCTableViewCell" owner:nil options:nil];
        cell=array[0];
    }
    
    VideoModel *vModel=[self.dataArray objectAtIndex:indexPath.row];
    [cell.videoHeadImageView sd_setImageWithURL:[NSURL URLWithString:vModel.video_thumb] placeholderImage:[UIImage imageNamed:@"noOpenPic"]];
    cell.videoTitleLabel.text=vModel.video_title;
    
    cell.playBtn.tag=indexPath.row;
    [cell.playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}

-(void)playBtnClick:(UIButton *)button
{
    
    VideoModel *vModel=[self.dataArray objectAtIndex:button.tag];
    NSURL *videoURL=[NSURL URLWithString:vModel.video_url];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BCPlay" object:videoURL];
    NSDictionary *dict=@{@"videoName":vModel.video_title};
    [MobClick event:@"playBtn" attributes:dict];
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);//将单元格的分界线抵拢边界
}

@end
